package com.demo.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.dao.KeShiBiaoMapper;
import com.demo.dao.PingJiaMapper;
import com.demo.dao.UserMapper;
import com.demo.dao.YiShengMapper;
import com.demo.dao.YiShengPaiBanMapper;
import com.demo.dao.YuYueJiuZhengMapper;
import com.demo.dao.YuYueTimeMapper;
import com.demo.entity.KeShiBiao;
import com.demo.entity.PingJia;
import com.demo.entity.User;
import com.demo.entity.YiSheng;
import com.demo.entity.YiShengPaiBan;
import com.demo.entity.YuYueJiuZheng;
import com.demo.entity.YuYueTime;

@Service
public class JiuzhengService{
	@Autowired
	private YiShengMapper yiShengMapper;
	
	@Autowired
	private YiShengPaiBanMapper yiShengPaiBanMapper;
	
	@Autowired
	private KeShiBiaoMapper keShiBiaoMapper;
	
	@Autowired
	private YuYueJiuZhengMapper yuYueJiuZhengMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private YuYueTimeMapper yuYueTimeMapper;
	
	@Autowired
	private PingJiaMapper pingJiaMapper;

	public int deleteByPrimaryKey(Integer id) {
		return yiShengMapper.deleteByPrimaryKey(id);
	}

	public int insert(YiSheng record){
		return yiShengMapper.insert(record);
	}

	public YiSheng selectByPrimaryKey(Integer id){
		return yiShengMapper.selectByPrimaryKey(id);
	}

	public List<YiSheng> selectAll(){
		return yiShengMapper.selectAll();
	}

	public int updateByPrimaryKey(YiSheng record){
		return yiShengMapper.updateByPrimaryKey(record);
	}
    
	public List<YiSheng> selectRandomNews(){
		return yiShengMapper.selectAll();
	}
	
	public List<YiSheng> findyishengBykeshi(String keshi){
		return yiShengMapper.findyishengBykeshi(keshi);
	}
	
	public int deleteByPrimaryKeyp(Integer id) {
		return yiShengPaiBanMapper.deleteByPrimaryKey(id);
	}

	public int insertp(YiShengPaiBan record){
		return yiShengPaiBanMapper.insert(record);
	}

	public YiShengPaiBan selectByPrimaryKeyp(Integer id){
		return yiShengPaiBanMapper.selectByPrimaryKey(id);
	}

	public List<YiShengPaiBan> selectAllp(){
		return yiShengPaiBanMapper.selectAll();
	}

	public int updateByPrimaryKeyp(YiShengPaiBan record){
		return yiShengPaiBanMapper.updateByPrimaryKey(record);
	}
    
	public List<YiShengPaiBan> findYishengByShijian(String jiuzhengshijian){
		return yiShengPaiBanMapper.findYishengByShijian(jiuzhengshijian);
	}
    
	public List<YiShengPaiBan> findYishengByYisheng(String yisheng){
		return yiShengPaiBanMapper.findYishengByYisheng(yisheng);
	}
    
	public  List<YiShengPaiBan> findYishengPaiban(String yisheng, String jiuzhengshijian){
		return yiShengPaiBanMapper.findYishengPaiban(yisheng, jiuzhengshijian);
	}
     
	public int deleteByPrimaryKeyk(Integer id) {
		return keShiBiaoMapper.deleteByPrimaryKey(id);
	}

	public int insertk(KeShiBiao record){
		return keShiBiaoMapper.insert(record);
	}

	public KeShiBiao selectByPrimaryKeyk(Integer id){
		return keShiBiaoMapper.selectByPrimaryKey(id);
	}

	public List<KeShiBiao> selectAllk(){
		return keShiBiaoMapper.selectAll();
	}

	public int updateByPrimaryKeyk(KeShiBiao record){
		return keShiBiaoMapper.updateByPrimaryKey(record);
	}
	
	public String quguahao(String yonghu,String yisheng,String riqi,String shijian,int feiyongflag,int okflag,String zhengzhuang){
		String ff = "";
		YuYueJiuZheng yueJiuZheng = new YuYueJiuZheng();
		int id=0;
		if(yuYueJiuZhengMapper.findMaxId()==null) {
			id = 90001;
		}else {
			id = Integer.valueOf(yuYueJiuZhengMapper.findMaxId())+1;
		}
		//String number = String.format("%04d", 12);  
		//System.out.println(number);
		//System.out.println(new Date().getTime());
		yueJiuZheng.setId(id);
		yueJiuZheng.setYonghu(yonghu);
		yueJiuZheng.setYisheng(yisheng);
		yueJiuZheng.setRiqi(riqi);
		yueJiuZheng.setShijian(shijian);
		yueJiuZheng.setFeiyongflag(feiyongflag);
		yueJiuZheng.setOkflag(okflag);
		yueJiuZheng.setZhengzhuang(zhengzhuang);
		yueJiuZheng.setDeleteflag(0);
		SimpleDateFormat dSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		yueJiuZheng.setGuahaodt(dSimpleDateFormat.format(new Date()));
		System.out.println(yueJiuZheng.toString());
		if(checkCHongfuGuahao(yueJiuZheng)) {
			if(checkYiBeiYuyue(yueJiuZheng)) {
				if(yuYueJiuZhengMapper.insert(yueJiuZheng)==1) {
					ff = "SUCCESS";
				}else {
					ff = "SB";
				}
			}else {
				ff = "YBY";
			}
		}else {
			ff = "CF"; //重复挂号
		}
		return ff;
	}
	
	public boolean checkCHongfuGuahao(YuYueJiuZheng yyjz) {
		int aa = yuYueJiuZhengMapper.checkCHongfuGuahao(yyjz.getYonghu(), yyjz.getRiqi(), yyjz.getOkflag());
		if(aa>0) {
			return false;
		}else {
			return true;
		}
	}
	
	public boolean checkYiBeiYuyue(YuYueJiuZheng yyjz) {
		int aa = yuYueJiuZhengMapper.checkYiBeiYuyue(yyjz.getYisheng(), yyjz.getRiqi(), yyjz.getOkflag());
		if(aa>0) {
			return false;
		}else {
			return true;
		}
	}
	
	public List<YuYueJiuZheng> myYuYue(String yonghu){
		System.out.println(yonghu+"yonghu");
		List<YuYueJiuZheng> list = yuYueJiuZhengMapper.myYuYue(yonghu);
		List<YuYueJiuZheng> listre = new ArrayList<YuYueJiuZheng>();
		if(list.size()>0) {
			for (YuYueJiuZheng yuYueJiuZheng : list) {
				YuYueJiuZheng yuYueJiuZheng2 = yuYueJiuZheng;
				System.out.println(yuYueJiuZheng2.toString());
				int yishengid = (yuYueJiuZheng.getYisheng()!=""&&yuYueJiuZheng.getYisheng()!=null)?Integer.valueOf(yuYueJiuZheng.getYisheng()):0;
				if(yishengid!=0) {
					YiSheng yiSheng = yiShengMapper.selectByPrimaryKey(yishengid);
					if(yiSheng!=null) {
						yuYueJiuZheng2.setDoctorName(yiSheng.getName());
						yuYueJiuZheng2.setKeshiName(keShiBiaoMapper.selectByPrimaryKey(
								Integer.valueOf(yiSheng.getKeshi())).getKeshi());
					}
				}
				if(yonghu!=""&&yonghu!="null"&&yonghu!=null) {
					User user = userMapper.selectByPrimaryKey(Integer.valueOf(yonghu));
					yuYueJiuZheng2.setYonghuName(user.getName());
				}
				listre.add(yuYueJiuZheng2);
			}
		}
		return listre;
	}

	public int tuihao(int id) {
		YuYueJiuZheng  yueJiuZheng = yuYueJiuZhengMapper.selectByPrimaryKey(id);
		if(yueJiuZheng!=null) {
			yueJiuZheng.setDeleteflag(1);
			SimpleDateFormat dSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			yueJiuZheng.setTuihaodt(dSimpleDateFormat.format(new Date()));
			return yuYueJiuZhengMapper.updateByPrimaryKey(yueJiuZheng);
		}
		return 0;
	}

	public List<YuYueTime>  findAlTimes(String yisheng,String date){
		List<YuYueTime>  resList = new ArrayList<YuYueTime>();
		System.out.println(yisheng+"---"+date);
		List<YuYueJiuZheng> listyJiuZhengs = yuYueJiuZhengMapper.findRecoredByDocADate(yisheng, date);
		System.out.println(listyJiuZhengs.size()+"listyJiuZhengs.size()");
		if(listyJiuZhengs.size()==0) {
			for (YuYueTime time : yuYueTimeMapper.selectAll()) {
				YuYueTime timeaTime = time;
				timeaTime.setYuyuecan(true);
				resList.add(timeaTime);
			}
			return resList;
		}else {
			List<String> aList = new ArrayList<String>();
			for (YuYueJiuZheng jiuZheng : listyJiuZhengs) {
				if(jiuZheng.getDeleteflag()!=null&&jiuZheng.getDeleteflag()!=1) {
					int aa = jiuZheng.getOkflag();
					if(aa>0&&aa<10) {
						aList.add("0"+aa);
					}else {
						aList.add(String.valueOf(aa));
					}
				}
			}
			System.out.println("AAA"+aList.toString());
			if(aList.size()>0) {
				List<YuYueTime> list =  yuYueTimeMapper.selectAll();
				for (YuYueTime yuYueTime2 : list) {
					YuYueTime yTime = yuYueTime2;
					yTime.setYuyuecan(true);
					for (String beiyueNumber : aList) {
						if(beiyueNumber.equals(yuYueTime2.getNumber())) {
							yTime.setYuyuecan(false);
							break;
						}
					}
					resList.add(yTime);
				}
			}
			return resList;
		}
		
	}

	public YuYueJiuZheng topingjia1(int id) {
		return yuYueJiuZhengMapper.selectByPrimaryKey(id);
	}
	
	public int addpingjia(String jiuzhengbianhao,String score,String jianyi) {
		PingJia pingJia = new PingJia();
		pingJia.setJiuzhengbianhao(jiuzhengbianhao);
		pingJia.setScore(score);
		pingJia.setJianyi(jianyi);
		return pingJiaMapper.insert(pingJia);
	}

	public PingJia checkpingjiaexist(String jiuzhengbianhao) {
		return pingJiaMapper.selectByid(jiuzhengbianhao);
	}
	
}
